package cn.usian.service.impl;

import cn.usian.fegin.ProductSkuFegin;
import cn.usian.jyzx.model.entity.h5.CartInfo;
import cn.usian.jyzx.model.entity.product.ProductSku;
import cn.usian.jyzx.model.entity.user.UserInfo;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;
import cn.usian.service.CartService;
import com.alibaba.fastjson.JSON;
import cn.util.AuthThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartServiceimpl implements CartService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    ProductSkuFegin productSkuFegin;
    @Override
    public void toCart(Long stuId, Integer stuNum){
        UserInfo userInfo = AuthThreadLocalUtils.get();
        //传入的小value
        ProductSku productSku = productSkuFegin.findById(stuId);

        String string = (String) redisTemplate.boundHashOps("cart" + userInfo.getId() + "").get(productSku.getId()+"");
        CartInfo parsed = JSON.parseObject(string, CartInfo.class);
        if (parsed!=null){
            parsed.setSkuNum(stuNum+1);
            redisTemplate.boundHashOps("cart"+userInfo.getId()+"").put(productSku.getId()+"",JSON.toJSONString(parsed));
            return;
        }

        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userInfo.getId());
        cartInfo.setSkuId(productSku.getId());
        cartInfo.setCartPrice(productSku.getSalePrice());
        cartInfo.setSkuNum(stuNum);
        cartInfo.setImgUrl(productSku.getThumbImg());
        cartInfo.setSkuName(productSku.getSkuName());
        cartInfo.setIsChecked(1);


        redisTemplate.boundHashOps("cart"+userInfo.getId()+"").put(productSku.getId()+"", JSON.toJSONString(cartInfo));
    }

    @Override
    public Result<List<CartInfo>> cartList() {
        UserInfo userInfo = AuthThreadLocalUtils.get();
        List<Object> cartList= redisTemplate.boundHashOps("cart"+userInfo.getId()+"").values();
            List<CartInfo> cartInfoList = cartList.stream().map(car -> JSON.parseObject(car.toString(), CartInfo.class)).collect(Collectors.toList());
            return Result.build(cartInfoList,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result deleteCart(Integer id) {
        UserInfo userInfo = AuthThreadLocalUtils.get();
        redisTemplate.boundHashOps("cart"+userInfo.getId()).delete(id+"");
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result checkCart(Integer id, Integer isChecked) {
        UserInfo userInfo = AuthThreadLocalUtils.get();
        Object o = redisTemplate.boundHashOps("cart" + userInfo.getId()).get(id + "");
        CartInfo cartInfo = JSON.parseObject(o.toString(), CartInfo.class);
        cartInfo.setIsChecked(isChecked);
        redisTemplate.boundHashOps("cart" + userInfo.getId()).put(id+"",JSON.toJSONString(cartInfo));

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result allCheckCart(Integer isChecked) {
        UserInfo userInfo = AuthThreadLocalUtils.get();
        List<Object> values = redisTemplate.opsForHash().values("cart" + userInfo.getId());
        values.stream().map(val -> JSON.<CartInfo>parseObject(val.toString(), CartInfo.class))
                .forEach( val ->{
                    val.setIsChecked(isChecked);
                    redisTemplate.boundHashOps("cart"+userInfo.getId()).put(val.getSkuId()+"",JSON.toJSONString(val));
        });




        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result clearCart() {
        Long id = AuthThreadLocalUtils.get().getId();
        redisTemplate.delete("cart"+id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
